<?php
require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_bet_history.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_match.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_industry.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_match_choice.php";
require_once DOCUMENT_ROOT . DIR_SHARED_DAO . "dao_choice.php";
require_once DOCUMENT_ROOT . DIR_SHARED_API . "api_general.php";
require_once DOCUMENT_ROOT . DIR_SHARED_API . "api_bet_calculators.php";
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_date.php';

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of api_security
 *
 * @author Thien
 */
class api_matches {
    
    public static function get_all_matches_live() {
        $dao_match = new dao_match();
        $now = lib_date::get_now();
        $temp = DateTime::createFromFormat("Y-m-d H:i:s", $now);
        $now = date_format($temp, "Y-m-d H:i");
        $live_matches = $dao_match->get_all_live_in($now);
        return $live_matches;
    }
    
    public static function get_type_id_by_match_id($match_id) {
        $dao_match = new dao_match();
        $match = $dao_match->get_by_id($match_id);
        return $match['type_id'];
    }
    
    public static function save_match($match_id, $name, 
                $description, $start_date, $end_date, 
                $country_code, $industry_id, $choice_texts, $win_choice_id) {
        $dao_match = new dao_match();
        $inserted_id = $dao_match->save($match_id, $name, $description, 
                lib_date::convert_to_sql_date_time($start_date, 'Y/m/d H:i'), 
                lib_date::convert_to_sql_date_time($end_date, 'Y/m/d H:i'), 
                $country_code, api_country::get_name_by_code($country_code), 
                $industry_id, self::get_commission(), 0, $win_choice_id);
        
        $dao_match_choice = new dao_match_choice();
        $dao_match_choice->delete_by_match_id($match_id);
        $dao_choice = new dao_choice();
        foreach ($choice_texts as $choice_text) {
            if (!empty($choice_text)) {
                $temp_id = $match_id;
                if ($match_id == 0) {
                    $temp_id = $inserted_id;
                }

                $choice = $dao_choice->get_by_name($choice_text);
                if (empty($choice)) {
                    $choice_id = $dao_choice->save(0, $choice_text);
                } else {
                    $choice_id = $choice['id'];
                }
                $dao_match_choice->save(0, $temp_id, $choice_id);
            }
        }
        if (!empty($win_choice_id)) {
            api_bet_calculators::finalize_match($match_id, $win_choice_id);
        }
        return TRUE;
    }
    
    public static function is_still_live($match_id) {
        $dao_match = new dao_match();
        $now = lib_date::get_now();
        $temp = DateTime::createFromFormat("Y-m-d H:i:s", $now);
        $now = date_format($temp, "Y-m-d H:i");
        $match = $dao_match->get_match_live_by_id($match_id, $now);
        if (!empty($match) && empty($match['win_choice_id'])) {
            return TRUE;
        }
        return FALSE;
    }
    
    public static function get_commission() {
        $option = api_general::get_option("commission");
        return $option;
    }
    
    public static function get_all_matches() {
        $dao_match = new dao_match();
        $matches = $dao_match->get_all_normal();
        return $matches;
    }
    
    public static function get_all_industries() {
        $dao_industry = new dao_industry();
        $list = $dao_industry->get_all();
        return $list;
    }
    
    public static function get_all_choices() {
        $dao_choice = new dao_choice();
        $list = $dao_choice->get_all();
        return $list;
    }
    
    public static function get_number_of_choice_by_match_id($match_id) {
        $dao_match_choice = new dao_match_choice();
        $number = $dao_match_choice->count_all_by_match_id($match_id);
        return $number;
    }
    
    public static function get_all_matches_hottest() {
        $dao_match = new dao_match();
        $now = lib_date::get_now();
        $live_matches = $dao_match->get_all_hottest($now);
        return $live_matches;
    }
    
    public static function get_match_by_id($match_id) {
        $dao_match = new dao_match();
        $item = $dao_match->get_by_id($match_id);
        return $item;
    }
    
    public static function get_match_choices_by_match_id($match_id) {
        $dao_match_choice = new dao_match_choice();
        $list = $dao_match_choice->get_all_by_match_id($match_id);
        return $list;
    }
    
    public static function delete_match($match_id) {
        $dao_match = new dao_match();
        $dao_match->delete($match_id);
        return TRUE;
    }
    
    public static function validate_match_fields($name, 
                $description, $start_date, $end_date, 
                $country_code, $industry_id, $choice_texts) {
        $error_show = "";
        if (empty($name)) {
            $error_show .= "Please fill Name.";
        } else
        if (empty($description)) {
            $error_show .= "Please fill Description.";
        } else
        if (empty($start_date)) {
            $error_show .= "Please choose a Start Date.";
        } else
        if (empty($end_date)) {
            $error_show .= "Please choose a End Date.";
        } else
        if (empty($country_code)) {
            $error_show .= "Please choose a Country Code.";
        } else
        if (empty($industry_id)) {
            $error_show .= "Please choose a Industry.";
        } else
        if (empty($choice_texts)) {
            $error_show .= "Please add Choices.";
        }
//        if (empty($end_date)) {
//            $error_show .= "Please choose a End Date must be after Start Date.";
//        } else
        return $error_show;
    }
}
